﻿// <copyright file="Parameter.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace MediaRss
{
    #region

    using System.Xml;
    using System.Xml.Serialization;

    #endregion

    [XmlRoot(ElementName = "param", Namespace = "http://search.yahoo.com/mrss/")]
    public class Parameter : MediaRssBase, IXmlSerializable
    {
        #region Constants and Fields

        internal const string ELEMENT_NAME = "param";

        #endregion

        #region Public Properties

        public string Content { get; set; }

        public string Name { get; set; }

        #endregion

        #region Public Methods

        public void ReadXml(XmlReader reader)
        {
            bool isEmptyElement = reader.IsEmptyElement;
            if (reader.HasAttributes)
            {
                for (int i = 0; i < reader.AttributeCount; i++)
                {
                    reader.MoveToNextAttribute();
                    if (reader.NamespaceURI == "")
                    {
                        if (reader.LocalName == "name")
                        {
                            this.Name = reader.Value;
                        }
                        else
                        {
                            base.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                        }
                    }
                }
            }
            reader.ReadStartElement();
            if (!isEmptyElement)
            {
                this.Content = reader.ReadContentAsString().Trim();
            }
            reader.ReadEndElement();
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("media", "param", "http://search.yahoo.com/mrss/");
            if (!string.IsNullOrEmpty(this.Name))
            {
                writer.WriteAttributeString("name", null, this.Name);
            }
            if (!string.IsNullOrEmpty(this.Content))
            {
                writer.WriteString(this.Content);
            }
            foreach (var pair in base.AttributeExtensions)
            {
                writer.WriteAttributeString(pair.Key.Name, pair.Key.Namespace, pair.Value);
            }
            foreach (var element in base.ElementExtensions)
            {
                element.WriteTo(writer);
            }
            writer.WriteEndElement();
        }

        #endregion
    }
}